package net.sytes.hamir.importadorchess.pgnreader;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.regex.Pattern;

import net.sytes.hamir.importadorchess.Game;

public abstract class AbstractReader {
	
	private static final Pattern OPENING_PATTERN = Pattern.compile("^\\[Event[^]]+\\]$");
	
	public abstract Collection<Game> read();	
	
	public Collection<Game> processPGN(InputStream in) throws IOException {
		Collection<Game> games = new ArrayList<Game>();
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		
		String line;
		Game currentGame = null;
		while((line = reader.readLine()) != null){
			if(OPENING_PATTERN.matcher(line).matches()){
				currentGame = new Game();
				games.add(currentGame);
			}
			if(currentGame != null){
				currentGame.processLine(line);
			}
		}
		return games;
	}

}
